<?php

namespace app\admin\controller;
use app\admin\controller\Main;
use think\Controller;
use app\commons\model\Member as MemberModel;
use think\Db;

class Member extends Main {

    public function index() {
        $_GPC = input();
        $page = max(intval($_GPC['page']), 1);
        $limit = max(intval($_GPC['limit']), 1);
        $row = new MemberModel();
        if (!empty($_GPC['id'])) {
            $row = $row->where('id', $_GPC['id']);
        }
        if (!empty($_GPC['nickname'])) {
            $row = $row->whereLike('nickname',"{$_GPC['nickname']}%");
        }
        if (!empty($_GPC['openid'])) {
            $row = $row->whereLike('openid', "%{$_GPC['openid']}%");
        }
        if (isset($_GPC['gender']) && $_GPC['gender'] != '') {
            $row = $row->where('gender', $_GPC['gender']);
        }
        if (!empty($_GPC['mobile'])) {
            $row = $row->whereLike('mobile', "%{$_GPC['mobile']}%");
        }
        $count = $row->count();
        $data = $row->page($page, $limit)->order('createtime', 'DESC')->select();
        return jsonpp($data, $count);
    }

    public function edit() {
        $_GPC = input();
        $id = $_GPC['id'];
        if ($this->request->isPost()) {
            $token = check_formtoken();
            unset($_GPC['data']['token']);
            $result = Db::name('member')->where(array('id' => $id))->update($_GPC['data']);
            if (!empty($result) || $result === 0) {
                json(array('token' => $token));
            } else {
                json(array('token' => $token), 1, '提交失败！');
            }
        }
        $data = Db::name('member')->where(array('id' => $id))->find();
        return jsonp(get_upload_data($data));
    }

    public function del() {
        $_GPC = input();
        $id = $_GPC['id'];
        if ($this->request->isPost()) {
            //删除用户数据
            $result = MemberModel::destroy(function($query) use ($id){
                $query->where('id','in',$id);
            });
            if($result){
                jsont([],0,'删除成功！');
            }
            jsont([], 1, '删除失败');
        }
        json([], 1, 'error');
    }

}
